﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RayDen.Library.Core.Primitives;
using RayDen.SceneLab.UI;

namespace RayDen.SceneLab.Controllers.FormControllers
{
    public class ColorEditController : FormControllerBase<ColorEditForm>
    {

        public float[] SpectralData;

        public Color SelectedColor;

        public Color? ConvertedColor;

        public RgbSpectrum RgbSpectrumColor
        {
            get { return RgbSpectrum.Create(this.SelectedColor); }
        }

        public SampledSpectrum PointSampledSpectrumRefl
        {
            get { return new SampledSpectrum(RgbSpectrumColor, SpectrumType.Reflectance); }
        }
        public SampledSpectrum PointSampledSpectrumIllum
        {
            get { return new SampledSpectrum(RgbSpectrumColor, SpectrumType.Illuminant); }
        }

        public RegularSPD RegularSPD_Refl
        {
            get
            {
                return new RegularSPD(this.PointSampledSpectrumRefl.ToArray(), SampledSpectrum.sampledLambdaStart, SampledSpectrum.sampledLambdaEnd, SampledSpectrum.nSpectralSamples);
            }
        }
        public RegularSPD RegularSPD_illum
        {
            get
            {
                return new RegularSPD(this.PointSampledSpectrumRefl.ToArray(), SampledSpectrum.sampledLambdaStart, SampledSpectrum.sampledLambdaEnd, SampledSpectrum.nSpectralSamples);
            }
        }
        public ColorEditController(Form owner)
            : base(owner)
        {
        }

        internal void OpenSPDFile(string p)
        {
            SpectralData = SPD_Data.FromFile(p).Select(item => (float)(item)).ToArray();
        }

        public Color ConvertRegular(float[] wl)
        {
            var nSamples = wl.Length / 2;
            var lambda = new float[nSamples];
            var val = new float[nSamples];

            for (int index = 0; index < lambda.Length; index++)
            {
                lambda[index] = wl[index * 2];
                val[index] = wl[index * 2 + 1];
            }

            var spd = new RegularSPD(val, SampledSpectrum.CIEstart, SampledSpectrum.CIEend,val.Length);
            spd.Normalize();
            return spd.ToRgb().ToColor();
        }

        public Color ConvertSampled(float[] wl, float[] lb = null, bool refl = true)
        {

            var nSamples = wl.Length/2;
            var lambda = new float[nSamples];
            var val = new float[nSamples];
            
            for (int index = 0; index < lambda.Length; index++)
            {
                lambda[index] = wl[index*2];
                val[index] = wl[index*2 + 1];
            }

            return SampledSpectrum.FromSampled(lambda, val, refl ? SpectrumType.Reflectance : SpectrumType.Illuminant).Transform();
        }


        public Color ConvertIrregular(float[] wl)
        {

            var nSamples = wl.Length / 2;
            var lambda = new float[nSamples];
            var val = new float[nSamples];

            for (int index = 0; index < lambda.Length; index++)
            {
                lambda[index] = wl[index * 2];
                val[index] = wl[index * 2 + 1];
            }

            var spd = new IrregularSPD(lambda, val, lambda.Length);
            spd.Normalize();

            return spd.ToRgb().ToColor();
        }
    }
}
